package com.umfwechat.lightprogram.controller.selfdeploy;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.constant.CacheConsts;
import com.umfwechat.lightprogram.vo.SetAgentCfg;
import com.umfwechat.lightprogram.vo.SetAgentRateCfg;
import com.umfwechat.util.PropertyUtil;
import com.umfwechat.util.RedisUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.SaasPropertyUtil;
import com.umfwechat.util.StringUtil;
import com.umfwechat.wx.service.WeixinService;
import com.umpay.operation.model.qrConf.QrConf;
import com.umpay.operation.service.qrConf.remote.QrConfServiceRemote;
import com.umpay.operation.xml.XmlData;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.saas.AppChannel;
import com.umpay.sp.model.saas.RatesConf;
import com.umpay.sp.model.saas.StoreInf;
import com.umpay.sp.service.saas.remote.AppChannelServiceRemote;
import com.umpay.sp.service.saas.remote.RatesServiceRemote;
import com.umpay.sp.service.saas.remote.StoreInfServiceRemote;
import com.umpay.sp.service.sys.remote.UserServiceRemote;


/**
 * 立马付
 * @author llb
 *
 */
@Controller
@RequestMapping("/merchantDeploy")
public class MerchantImPayController {
	private static Logger logger = LoggerFactory.getLogger(MerchantImPayController.class);
	
	//渠道service
	@Resource
	private AppChannelServiceRemote appChannelServiceRemote;
	//二维码绑定service
	@Resource
	private QrConfServiceRemote qrConfServiceRemote;
	//代理商费率配置
	@Resource
	private RatesServiceRemote ratesServiceRemote;
	//店铺部署
	@Resource
	private StoreInfServiceRemote storeInfServiceRemote;
	//用户信息server
	@Resource
	private UserServiceRemote userServiceRemote_sp;
	//生成二维码service
	@Resource
	private WeixinService weixinService; 
	
	@RequestMapping("/openImPay")
	@ResponseBody
	public JSONObject openImPay(HttpServletRequest request, HttpServletResponse response) throws Exception{		
		JSONObject json= new JSONObject();
		json.put("retCode", "0000");
		json.put("retMsg", "成功");
		String reqJSON=  (String) request.getAttribute("_requestData");
		JSONObject param=JSONObject.fromObject(reqJSON);
		String storeId = param.optString("storeId");	//店铺编号
		String wechatRate = param.optString("wechatRate");	//微信费率
		String alipayRate = param.optString("alipayRate");	//支付宝费率
		String qrCodeList0 = param.optString("qrCodeList");	//收款码列表
		if(qrCodeList0==null || "".equals(qrCodeList0)){	//收款码列表为空时，赋值为空数组字符串
			qrCodeList0="[]";
		}
		String agentId = param.optString("loginAgentId");	//代理商编号
//		
		JSONArray fromObject = JSONArray.fromObject(qrCodeList0);
		String[] qrCodeList=new String[fromObject.size()];	
		//创建一个数组，将前端传递的数组字符串放进去
		int i=0;
		for (Object object : fromObject) {
			qrCodeList[i]=object.toString();
			i++;
		}
		//校验联动给代理商设置的费率
		//TODO
		com.umfwechat.common.ReqMessage reqMessage = ReqMessageUtil.makeReq();
		Map<String,Object> reqMap = reqMessage.getReqMap();
		reqMap.put("rpid", reqMessage.getRpid());
		reqMap.put("reqDate", reqMessage.getReqDate());
		reqMap.put("reqTime", reqMessage.getReqTime());
		reqMap.put("agentId", agentId);
		String agentProfitType=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","agentProfitType", "");
		reqMap.put("type", agentProfitType);	//定价产品 type就传 10，传0时有数据方便测试
		Map<String,Object> frontResult = null;
		try {
			logger.info("调用联动给代理商设置的费率接口入参："+reqMap.toString());
			String agentProfitUrl=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","agentProfitUrl", "");	//http://10.10.77.88:18888
			frontResult = com.umpay.http.platform2.HttpUtil.httpPostForm2Xml
					(agentProfitUrl, reqMap);
			logger.info("调用联动给代理商设置的费率接口出参："+frontResult.toString());
		} catch (Exception e2) {
			json.put("retCode", "0001");
			json.put("retMsg","调用费率查询接口异常");
			logger.info("调用费率查询接口异常："+e2);
			return json;
		}
		String retCodeP2 = (String) frontResult.get("retCode");
		double lowRateStander=200d;
		if(retCodeP2.equals("0000")){
			String listString = (String)frontResult.get("list");
			if(listString != null){
				JSONArray jArray = JSONArray.fromObject(listString);
				List<SetAgentRateCfg> list =jArray.toList(jArray, SetAgentRateCfg.class);
				//循环查询到的费率，将最低值中的较大者取出来
				logger.info("调用联动给代理商设置的费率list："+list.toString());
				//循环查询到的费率，将最低值中的较大者取出来
				for (SetAgentRateCfg sa : list) {
					String sDate = sa.getStartDate();
					String eDate = sa.getEndDate();
					DateFormat df=new SimpleDateFormat("yyyyMMdd");
					long startDate = df.parse(sDate).getTime();
					long endDate = df.parse(eDate).getTime();
					long nowDate=new Date().getTime();
					logger.info("有效开始时间"+startDate+"有效结束时间:"+endDate+"现在时间："+nowDate);
					if(startDate<=nowDate && nowDate<=endDate){
						if("25".equals(sa.getRuleValue())){
							double fratVal=Double.parseDouble((sa.getFratVal()));
							lowRateStander = fratVal*100000;	//"fratVal": "0.001234"	
						}
					}
				}
			}
		}else{
			json.put("retCode", "0001");
			json.put("retMsg","调用费率查询接口失败"+frontResult.get("retMsg"));
			return json;
		}
		logger.info("调用联动给代理商设置的费率接口得到最低费率为："+lowRateStander/100000);
		//查询代理商设置的立马付费率接口
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.put("agentId", agentId);
		ResMessage res = null;
		try {
			logger.info("查询代理商设置的立马付费率接口入参："+req.toString());
			res = ratesServiceRemote.getRatesByAgentId(req);
			logger.info("查询代理商设置的立马付费率接口出参："+res.toString());
		} catch (Exception e) {
			json.put("retCode", "0001");
			json.put("retMsg", "查询代理商费率配置异常，请稍后重试");
			logger.info("调用查询店铺列表异常失败："+e);
			return json;
		}
		//判断返回码是否是0000
		String code = res.getRetCode();
		if(code.equals("0000")){	
			Map<String, Object> map = res.getResMap();
			RatesConf ratesConf=(RatesConf) map.get("ratesConf");
			if(ratesConf==null){
				json.put("retCode", "0001");
				json.put("retMsg","没有查询到代理商上配置的费率信息");
				return json;
			}
			double highRate = Double.valueOf(ratesConf.getHighRate())*1000;
			double lowRate = Double.valueOf(ratesConf.getLowRate())*1000;
			double wxr=Double.valueOf(StringUtil.trim(wechatRate))*1000;
			if(wxr < lowRate || wxr >highRate || wxr<lowRateStander || wxr>10000){	//费率必须在代理商设置的最高和最低范围内，并且不能低于联动给代理商设置的最低费率，并且费率不得大于10%
				json.put("retCode", "0001");
				json.put("retMsg","设置的费率必须在"+lowRate/1000+"到"+highRate/1000+"范围内"+",并且费率大于等于"+lowRateStander/1000);
				return json;
			}
			double apr=Double.valueOf(StringUtil.trim(alipayRate))*1000;
			if(apr < lowRate || apr >highRate || apr<lowRateStander || apr>10000){
				json.put("retCode", "0001");
				json.put("retMsg","设置的费率必须在"+lowRate/1000+"到"+highRate/1000+"范围内"+",并且费率大于等于"+lowRateStander/1000);
				return json;
			}
			
			ReqMessage reqChannel = ReqMessageUtil.makeSpReq();
			reqChannel.put("appSysId", storeId);
			reqChannel.put("mainProductId", "S000040");
			//查询签约产品接口
			ResMessage resChannel =null;
			try {
				logger.info("查询签约产品接口入参："+reqChannel.toString());
				resChannel = appChannelServiceRemote.queryListAppChannel(reqChannel);
				logger.info("查询签约产品接口出参："+resChannel.toString());
			} catch (Exception e) {
				json.put("retCode", "0001");
				json.put("retMsg","调用渠道列表查询接口异常");
				logger.info("调用渠道列表查询接口异常："+e);
				return json;
			}
			//判断返回码是否是0000
			String codeChannel = resChannel.getRetCode();
			if(codeChannel.equals("0000")){	
				//已经申请过签约,就修改签约
				ReqMessage reqChannelX = ReqMessageUtil.makeSpReq();
				reqChannelX.put("appSysId", storeId);
				//String productId=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","productId_impay", "");
				List<AppChannel> resultList = (List<AppChannel>) resChannel.getValue("resultList");
				reqChannelX.put("productId", resultList.get(0).getProductId());
//				reqChannelX.put("state", "4"); //状态不做处理
				reqChannelX.put("appName", "立马付");
				reqChannelX.put("mainProductId", "S000040");
				
				reqChannelX.put("alipayRates",String.valueOf((int)apr));	//c存整数，0.38%=38/10000
				reqChannelX.put("wecatRates",String.valueOf((int)wxr));
				ResMessage resChannelX =null;
				try {
					logger.info("修改签约产品接口入参："+reqChannelX.toString());
					resChannelX=appChannelServiceRemote.updateChannel(reqChannelX);
					logger.info("修改签约产品接口出参："+resChannelX.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","调用渠道修改接口异常");
					logger.info("调用渠道修改接口异常："+e);
					return json;
				}
				//判断返回码是否是0000
				String codeChannelX = resChannelX.getRetCode();
				if(!codeChannelX.equals("0000")){	
					json.put("retCode", "0001");
					json.put("retMsg","渠道修改接口修改失败");
					return json;
				}
			}else{
				//没有申请过签约，就申请签约产品
				ReqMessage reqChannelS = ReqMessageUtil.makeSpReq();
				reqChannelS.put("appSysId", storeId);
				String productId=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","productId_impay", "");
				reqChannelS.put("productId", productId);
				reqChannelS.put("state", "4");	
				reqChannelS.put("appName", "立马付");
				reqChannelS.put("mainProductId", "S000040");
				reqChannelS.put("alipayRates", String.valueOf((int)apr));
				reqChannelS.put("wecatRates", String.valueOf((int)wxr));
				ResMessage resChannelS =null;
				try {
					logger.info("插入签约产品接口入参："+reqChannelS.toString());
					resChannelS=appChannelServiceRemote.insertChannel(reqChannelS);
					logger.info("插入签约产品接口出参："+resChannelS.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","调用渠道开通接口异常");
					logger.info("调用渠道开通接口异常："+e);
					return json;
				}
				//判断返回码是否是0000
				String codeChannelS = res.getRetCode();
				if(!codeChannelS.equals("0000")){	
					json.put("retCode", "0001");
					json.put("retMsg","渠道开通接口开通失败");
					return json;
				}
			}
			//校验收款码是否已经绑定过，即收款码是否可用
			List<String> qrCodes=new ArrayList<String>();
			for (String qrId : qrCodeList) {
				com.umpay.operation.common.ReqMessage reqQrConf = ReqMessageUtil.makeOpReq();
				reqQrConf.put("qrId", qrId);
				com.umpay.operation.common.ResMessage resreqQrConf =null;
				try {
					logger.info("查询收款码接口入参："+reqQrConf.toString());
					resreqQrConf=qrConfServiceRemote.queryQrConfByKey(reqQrConf);
					logger.info("查询收款码接口出参："+resreqQrConf.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","根据主键查询收款码接口异常");
					logger.info("根据主键查询收款码接口异常："+e);
					return json;
				}
				String retCodeQrConf = resreqQrConf.getRetCode();
				if(retCodeQrConf.equals("0000")){	//查询到了，就说明该收款码已经被绑定
					QrConf qrConf=(QrConf)resreqQrConf.getValue("qrConf");
					logger.info(qrId+"绑定的商户为"+qrConf);
					String merId = qrConf.getMerId();
					if("".equals(merId)){	//商户号没值，说明未绑定过，就将该收款码放入新的集合中
						qrCodes.add(qrId);
						continue;
					}else if(storeId.equals(merId)){	//判断二维码是否是被该店铺绑定的，如果是，就不放入新集合
						continue;
					}else{
						json.put("retCode", "0001");
						json.put("retMsg",qrId+"收款码已经商户"+merId+"被绑定");
						return json;
					}
				}else{
					json.put("retCode", "0001");
					json.put("retMsg",qrId+"该收款码无效");
					return json;
				}
			}
			//先查询店铺详细信息，绑定收款码时使用店铺名称
			ReqMessage reqStore = ReqMessageUtil.makeSpReq();
			reqStore.put("storeId", storeId);
			ResMessage resStore =null;
			try {
				logger.info("【根据店铺编号查询店铺信息接口】入参："+reqStore.toString());
				resStore = storeInfServiceRemote.getStoreInfByPri(reqStore);	//根据店铺编号查询店铺信息
				logger.info("【根据店铺编号查询店铺信息接口】出参："+resStore.toString());
			} catch (Exception e1) {
				json.put("retCode", "0001");
				json.put("retMsg","查询店铺详情接口异常");
				logger.info("查询店铺详情接口异常："+e1);
				return json;
			}
			String retCodeStore = resStore.getRetCode();
			StoreInf storeInf =null;
			if(retCodeStore.equals("0000")){
				storeInf = (StoreInf) resStore.getResMap().get("storeInf");
				if(storeInf==null){
					json.put("retCode", "0001");
					json.put("retMsg", "查询店铺详情没有查询到");
					return json;
				}
			}else{
				json.put("retCode", "0001");
				json.put("retMsg", "查询店铺详情没有查询到"+resStore.getRetMsg());
				return json;
			}
			//绑定收款码
			for (String qrId : qrCodes) {
				com.umpay.operation.common.ReqMessage reqQrConf = ReqMessageUtil.makeOpReq();
				reqQrConf.put("qrId", qrId);
				reqQrConf.put("qrState", "1");
				reqQrConf.put("merId", storeId);
				reqQrConf.put("merName", storeInf.getStoreShortName());	//商户名称
				reqQrConf.put("goodsName", "立马付");	//商品名称
				String notifyUrl = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","notifyurl", "");	
				reqQrConf.put("notifyUrl", notifyUrl);	//回调路径
				
				com.umpay.operation.common.ResMessage resreqQrConf =null;
				try {
					logger.info("绑定收款码接口入参："+reqQrConf.toString());
					resreqQrConf=qrConfServiceRemote.updateQrConfByQrId(reqQrConf);
					logger.info("绑定收款码接口出参："+resreqQrConf.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","根据主机查询收款码接口异常");
					logger.info("根据主机查询收款码接口异常："+e);
					return json;
				}
				String retCodeQrConf = resreqQrConf.getRetCode();
				if(!retCodeQrConf.equals("0000")){
					json.put("retCode", "0001");
					json.put("retMsg","绑定收款码失败"+resreqQrConf.getRetMsg());
					return json;
				}
			}
			
			//根据主商户号查询是否开通过主用户
			String isHaveOpenUser="0"; //是否开通过主用户，0没有开通，1开通过
			Integer userId=null;	//主用户id，二维码场景值使用
			ReqMessage reqUser = ReqMessageUtil.makeSpReq();
			reqUser.put("merFlag", storeInf.getMerId());
			reqUser.put("currentPage", "1");
			reqUser.put("pageSize", "4");
			ResMessage resUser =null;
			try {
				logger.info("【查询用户列表接口】入参："+reqUser.toString());
				resUser = userServiceRemote_sp.queryList(reqUser);	//根据主商户号查询是否创建过主用户
				logger.info("【查询用户列表接口】出参："+resUser.toString());
			} catch (Exception e) {
				json.put("retCode", "0001");
				json.put("retMsg","根据主商户号查询是否开通过主用户接口异常");
				logger.info("根据主商户号查询是否开通过主用户接口异常："+e);
				return json;
			}
			String retCodeUser = resUser.getRetCode();
			if(retCodeUser.equals("0000")){
				List<Map<String, Object>> resultList=(List<Map<String, Object>>)resUser.getResMap().get(XmlData.RESULT_LIST);
				if(resultList.size()==0){
					isHaveOpenUser="0";
				}else{
					isHaveOpenUser="1";
					Map<String, Object> resultmap = resultList.get(0);
					userId=(Integer)resultmap.get("userId");
				}
			}else{
				json.put("retCode", "0001");
				json.put("retMsg",resUser.getRetMsg());
				return json;
			}
			if(isHaveOpenUser.equals("0")){ //没有开通过主用户，就开通主用户，再开通子用户
				//开通主用户
				ResMessage resOpenUserM =null;
				try {
					resOpenUserM = openUser(storeInf.getMerId(), storeInf.getPayeeName(), storeInf.getPayeeName(), storeInf.getPayeeMobileId());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","添加管理员接口异常");
					logger.info("添加管理员接口异常："+e);
					return json;
				}
				String retCodeOpenUserM = resOpenUserM.getRetCode();
				if(retCodeOpenUserM.equals("0000")){
					userId=(Integer)resOpenUserM.getValue(XmlData.USERID);
				}else{
					json.put("retCode", "0001");
					json.put("retMsg","添加管理员接口失败"+resOpenUserM.getRetMsg());		//该server错误时没有返回信息，只有错误码
					return json;
				}
				//开通子用户
				ResMessage resOpenUserO =null;
				try {
					resOpenUserO = openUser(storeId, storeInf.getPayeeName(), storeInf.getPayeeName(), storeInf.getPayeeMobileId());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","添加管理员接口异常");
					logger.info("添加管理员接口异常："+e);
					return json;
				}
				String retCodeOpenUserO = resOpenUserO.getRetCode();
				if(!retCodeOpenUserO.equals("0000")){
					json.put("retCode", "0001");
					json.put("retMsg","添加管理员接口失败");		//该server错误时没有返回信息，只有错误码
					return json;
				}
			}else{	//开通过主用户，就判断是否开通过子用户
				//判断是否开通过子用户
				ReqMessage reqUserO = ReqMessageUtil.makeSpReq();
				reqUserO.put("merFlag", storeId);
				reqUserO.put("currentPage", "1");
				reqUserO.put("pageSize", "4");
				ResMessage resUserO =null;
				try {
					logger.info("【查询用户列表接口】入参："+reqUserO.toString());
					resUserO = userServiceRemote_sp.queryList(reqUserO);	//根据店铺编号查询是否创建过子用户
					logger.info("【查询用户列表接口】出参："+resUserO.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","根据店铺编号查询是否开通过子用户接口异常");
					logger.info("根据店铺编号查询是否开通过子用户接口异常："+e);
					return json;
				}
				String retCodeUserO = resUserO.getRetCode();
				if(retCodeUserO.equals("0000")){
					List<Map<String, Object>> resultList=(List<Map<String, Object>>)resUserO.getResMap().get(XmlData.RESULT_LIST);
					if(resultList.size()==0){	//没有开通过子用户
						//开通子用户
						ResMessage resOpenUserO =null;
						try {
							resOpenUserO = openUser(storeId, storeInf.getPayeeName(), storeInf.getPayeeName(), storeInf.getPayeeMobileId());
						} catch (Exception e) {
							json.put("retCode", "0001");
							json.put("retMsg","添加管理员接口异常");
							logger.info("添加管理员接口异常："+e);
							return json;
						}
						String retCodeOpenUserO = resOpenUserO.getRetCode();
						if(!retCodeOpenUserO.equals("0000")){
							json.put("retCode", "0001");
							json.put("retMsg","添加管理员接口失败");		//该server错误时没有返回信息，只有错误码
							return json;
						}
					}
				}else{
					json.put("retCode", "0001");
					json.put("retMsg",resUser.getRetMsg());
					return json;
				}
			}
			String state =storeInf.getRegisterProcsState();
			if(state.equals("04")){	//待开通产品时就修改店铺的状态为05待邀请商户
				ReqMessage reqStoreU = ReqMessageUtil.makeSpReq();
				storeInf.setIntime(null);
				storeInf.setModtime(null);
				storeInf.setRegisterProcsState("05");
				Map<String, String> storeMap = SaasPropertyUtil.javaBeanToMap(storeInf);
				reqStoreU.putAll(storeMap);
				//修改店铺状态
				ResMessage resStoreU =null;
				try {
					logger.info("【更新店铺接口】入参："+reqStoreU.toString());
					resStoreU = storeInfServiceRemote.updateStoreInf(reqStoreU);
					logger.info("【更新店铺接口】出参："+resStoreU.toString());
				} catch (Exception e) {
					json.put("retCode", "0001");
					json.put("retMsg","更新店铺状态接口异常");
					logger.info("更新店铺状态接口异常："+e);
					return json;
				}
				String retCodeStoreU = resStoreU.getRetCode();
				if(!retCodeStoreU.equals("0000")){	//没有更新成功
					json.put("retCode", "0001");
					json.put("retMsg",resStoreU.getRetMsg());
					return json;
				}
			}
			//生成公众号二维码
			Integer scene_id=userId;
			Integer time=24*60*60;
			String expireTime=time.toString();//24小时
			String qrCodeUrl;
			try {
				logger.info("【生成二维码接口】入参：scene_id="+userId+"expireTime="+expireTime);
				qrCodeUrl = weixinService.getTicketQrCode(expireTime,scene_id);
				logger.info("【生成二维码接口】出参："+qrCodeUrl);
			} catch (Exception e) {
				json.put("retCode", "0001");
				json.put("retMsg", "获取二维码异常，稍后重试");
				logger.info("获取二维码异常："+e);
				return json;
			}
			json.put("storeId", storeInf.getStoreId());
			json.put("payeeName", storeInf.getPayeeName());
			json.put("storeShortName", storeInf.getStoreShortName());
			json.put("qrCodeUrl", qrCodeUrl);
			StringBuffer sb = new StringBuffer();
			long currentTime = new Date().getTime();
			sb.append(userId).append(",").append(expireTime).append(",").append(String.valueOf(currentTime)).append(",").append(qrCodeUrl);
			RedisUtil.set(CacheConsts.REDIS_QRCODE+storeId, 2592000, sb.toString());	//缓存起来,缓存30天
			logger.info("redis缓存中保存的信息为key为："+storeId+"value为："+sb.toString());
		}else{
			json.put("retCode", "0001");
			json.put("retMsg",res.getRetMsg());
		}
		
		return json;
	}
	/**
	 * 测试添加管理员方法
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
//	@RequestMapping("/aa")
//	@ResponseBody
//	public JSONObject aa(String merId,HttpServletRequest request, HttpServletResponse response) throws Exception{		
//		JSONObject json =new JSONObject();
//		ResMessage openUser = openUser(merId, "xinqing", "xinqing", "13800138000");
//		json.put("openUser", openUser);
//		return json;
//	}
	/**
	 * 添加管理员
	 * @param req
	 * @throws Exception 
	 */
	private ResMessage openUser(String merId,String logName,String name, String telephoneNo) throws Exception{
		ReqMessage req=ReqMessageUtil.makeSpReq();
		req.put(XmlData.TELEPHONENO, telephoneNo);
		req.put(XmlData.MERID, merId);
		req.put(XmlData.LOGNAME, "admin");
		req.put(XmlData.NAME, name);
		//TODO 
		req.put("servId", merId);//商户编号
		req.put("servType", "11");// 业务类型 银行卡 11电子渠道 12 话费 13 U付14 兑换券 21 U付导入41
		req.put("role", "10");
		req.put(XmlData.LOGIN_PASSWD, "111111");
		ResMessage res =null;
		logger.info("【添加管理员接口】入参："+req.toString());
		res = userServiceRemote_sp.addAdminUser(req);
		logger.info("【添加管理员接口】出参："+res.toString());
		return res;
	}
	/**
	 * 展示立马付产品
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/imPayDisplay")
	@ResponseBody
	public JSONObject imPayDisplay(HttpServletRequest request, HttpServletResponse response) throws Exception{		
		JSONObject json= new JSONObject();
		json.put("retCode", "0000");
		json.put("retMsg", "成功");
		String reqJSON=  (String) request.getAttribute("_requestData");
//		String reqJSON = null;
//		try {
//			reqJSON = StringReaderUtils.readFromIO(request.getInputStream());
//			logger.info("立马付展示接口入参>>>" + reqJSON);
//		} catch (IOException e) {
//			json.put("retCode", "0001");
//			json.put("retMsg", "立马付展示接口入参获取失败");
//			logger.error("立马付展示接口入参获取异常", e);
//			return json;
//		}
		JSONObject param=JSONObject.fromObject(reqJSON);
		String storeId = param.optString("storeId");
		//查询该店铺开通的立马付费率信息
		ReqMessage req = ReqMessageUtil.makeSpReq();
		Map<String, String> reqMap = req.getReqMap();
		reqMap.put("appSysId", storeId);	//应用编号
		String productIddg=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","productId_impay_added", "");
		logger.info("取出配置文件参数productId_impay_added ="+productIddg);
		String productIdpl=PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","productId_impay", "");
		logger.info("取出配置文件参数productId_impay ="+productIdpl);
//		reqMap.put("productId", productId);	//产品编号
		reqMap.put("productIds", "'"+productIddg+"','"+productIdpl+"'");	//产品编号
		ResMessage resMessage =null;
		try {
			//resMessage = appChannelServiceRemote.queryByAppSysId(req);
			logger.info("【查询签约的产品信息】入参："+req.toString());
			resMessage = appChannelServiceRemote.getAppChannelListByProductids(req);
			logger.info("【查询签约的产品信息】出参："+resMessage.toString());
		} catch (Exception e) {
			json.put("retCode", "0001");
			json.put("retMsg", "查询签约的产品信息异常");
			logger.info("查询签约的产品信息异常"+e);
			return json;
		}
		//判断返回码是否是0000
		String retCode = resMessage.getRetCode();
		if(retCode.equals("0000")){	//查询到店铺数量>0
			Map<String, Object> map = resMessage.getResMap();
//			AppChannel appChannel = (AppChannel) map.get(com.umpay.sp.xml.XmlData.APPCHANNEL);	
			List<AppChannel> appChannelList = (List<AppChannel>) map.get("appChannelList");
			if(null==appChannelList||appChannelList.isEmpty()){
				json.put("alipayRate",null);
				json.put("wechatRate",null);
			}else{
				AppChannel appChannel =appChannelList.get(0);
				Integer wecatRates = appChannel.getWecatRates();	//微信费率
				Integer alipayRates =  appChannel.getAlipayRates();	//支付宝费率
				Double wxr=Double.valueOf(wecatRates)/1000;
				Double apr=Double.valueOf(alipayRates)/1000;
				json.put("alipayRate", apr);
				json.put("wechatRate", wxr);
			}
			
		}else{
			json.put("retCode", "0001");
			json.put("retMsg", resMessage.getRetMsg());
			return json;
		}
		//查询该店铺绑定的二维码信息
		com.umpay.operation.common.ReqMessage req1 = new com.umpay.operation.common.ReqMessage();
		req1.getReqMap().put("merId",storeId);
		com.umpay.operation.common.ResMessage resMessage1 =null;
		try {
			resMessage1 = qrConfServiceRemote.queryQrConfByMerId(req1);
		} catch (Exception e) {
			json.put("retCode", "0001");
			json.put("retMsg", "查询二维码信息异常");
			logger.info("查询二维码信息异常"+e);
			return json;
		}
		//判断返回码是否是0000
		String retCode1 = resMessage1.getRetCode();
		if(retCode1.equals("0000")){	//查询到店铺数量>0
			Map<String, Object> map1 = resMessage1.getResMap();
			List<QrConf> qrList = (List<QrConf>) map1.get("qrList");	//二维码列表
			if(qrList !=null && qrList.size()>0){
				String[] qrCodeList=new String[qrList.size()];
				int i=0;
				for (QrConf qrConf : qrList) {
					qrCodeList[i]=qrConf.getQrId();
					i++;
				}
				json.put("qrCodeList", qrCodeList);
			}else{
				json.put("qrCodeList", null);
			}
		}
		return json;
	}
	
}
